import drawing, graph, config
import time

class Simulator:
  
    def __init__(self, g, drawArea):
        self.graph = graph.graph(g=g)
        self.drawArea = drawArea
        self.px, self.py = -1, -1
    
    def motion(self, x, y):
        self.px, self.py = x, y
    
    def handSimExpose(self, widget, drawing):
        self.graph.update(time.time())

        
        
        edges = self.graph.getEdges()
        nodes = self.graph.getNodes()
        cars = self.graph.getCars()

        aRouteEdges = []
        for c in cars:
            if c.isInCar(self.px, self.py):
                aRouteEdges = c.route.getEdges()
                break
        
        for e in edges:
            sNode = e.startNode
            eNode = e.endNode
            color = config.edge.color
            if e in aRouteEdges:
                color = config.route.selColor
            drawing.drawLineA(sNode.x, sNode.y, eNode.x, eNode.y, e.width, color)
        
        
        for n in nodes:
            if n.hasLights:
                light = config.node.redLight
                if not n.isRedLight:
                    light = config.node.greenLight
                drawing.drawCircle(n.x, n.y, n.radius + 0.01, light)
            drawing.drawCircle(n.x, n.y, n.radius, config.node.normalColor)
            

        for c in cars:
            x, y = c.getXY()
            drawing.drawCircle(x, y, c.radius, config.car.color)

        #self.canvas.window.draw_drawable(self.gc, self.buffer, 0, 0, 0, 0, -1, -1)
                     
        
